package com.wdf.dota;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;

public class MainActivity extends TabActivity {

	public TabHost tabHost;
	public static final String TAB_STRATEGY="攻略";
	public static final String TAB_VIDEO="视频";
	public static final String TAB_FORUM="论坛";
	public static final String TAB_SELFINFO="我";

	
	public RadioGroup radioGroup;
	
	Button radionButton1;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_tab);
		MobclickAgent.updateOnlineConfig( this );
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.update(this);  //update apk from umeng
	
		
		try {

        tabHost=this.getTabHost();
        
        TabSpec ts1=tabHost.newTabSpec(TAB_STRATEGY).setIndicator(TAB_STRATEGY);
        Intent intent1 = new Intent(MainActivity.this,GameMenuActivity.class);
        ts1.setContent(intent1);
        tabHost.addTab(ts1);
        
        TabSpec ts2=tabHost.newTabSpec(TAB_FORUM).setIndicator(TAB_FORUM);
        Intent intent2 = new Intent(MainActivity.this,ForumMenuActivity.class);

        ts2.setContent(intent2);
        tabHost.addTab(ts2);
        TabSpec ts3=tabHost.newTabSpec(TAB_SELFINFO).setIndicator(TAB_SELFINFO);
        Intent intent3 = new Intent(MainActivity.this,AccountActivity.class);
        ts3.setContent(intent3);
        tabHost.addTab(ts3);
        

        
        radionButton1 = (Button)this.findViewById(R.id.radio_button0);
        
        this.radioGroup=(RadioGroup)findViewById(R.id.main_radio);
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(RadioGroup group, int checkedId) {
				
				switch(checkedId){
				case R.id.radio_button0:
					tabHost.setCurrentTabByTag(TAB_STRATEGY);
					break;
				case R.id.radio_button1:
					tabHost.setCurrentTabByTag(TAB_FORUM);
					break;
				case R.id.radio_button2:
					tabHost.setCurrentTabByTag(TAB_SELFINFO);
					break;
				}
			}
		});
   
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
       
	}
	

	 public void finish(){
	    
		 super.finish();
		
	 }
	 

	

	
	   public void onResume() {
		   super.onResume();
		   MobclickAgent.onResume(this);
		   exitTime = System.currentTimeMillis();
		   }
		   public void onPause() {
		   super.onPause();
		   MobclickAgent.onPause(this);
		   exitTime = System.currentTimeMillis();
		   }
		   

           
       
		
		private long exitTime = 0;
		
		public boolean dispatchKeyEvent(KeyEvent event) {
			  if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			    if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
			      this.exitApp();
			    }
			    return true;
			  }
			  return super.dispatchKeyEvent(event);
			}
			/**
			 * 退出程序
			 */
			private void exitApp() {
			  // 判断2次点击事件时间
			  if ((System.currentTimeMillis() - exitTime) > 2000) {
			    Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
			    exitTime = System.currentTimeMillis();
			  } else {
			    finish();
			  }
			}

}
